﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ContractBLL : IBaseInfo
    {
        private static readonly IAttachDA attachDA = DataAccess.CreateAttachDA();
        private static readonly IAttachDetailDA attachDetailDA = DataAccess.CreateAttachDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly IContractDA contractDA = DataAccess.CreateContractDA();
        private List<string> dataBaseID;
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private PubParasInfo pubParasInfo;
        private static readonly IRentWhDetailDA rentWhDetailDA = DataAccess.CreateRentWhDetailDA();

        public ContractBLL()
        {
            this.pubParasInfo = null;
        }

        public ContractBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ContractBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        private void Copy(string strContractID, ContractInfo contractInfo)
        {
            if ((contractInfo.RentWhDetailInfos != null) && (contractInfo.RentWhDetailInfos.Count > 0))
            {
                foreach (RentWhDetailInfo RWDI in contractInfo.RentWhDetailInfos)
                {
                    RWDI.ContractID = strContractID;
                    RWDI.CreatedBy = this.pubParasInfo.UserID;
                    rentWhDetailDA.InsertRentWhDetail(RWDI, this.dataBaseID[0]);
                }
            }
            if (((contractInfo.AttachInfos != null) && (contractInfo.AttachInfos.AttachDetailInfos != null)) && (contractInfo.AttachInfos.AttachDetailInfos.Count > 0))
            {
                contractInfo.AttachInfos.AttachID = strContractID + "CRT";
                contractInfo.AttachInfos.DocID = strContractID;
                contractInfo.AttachInfos.CreatedBy = this.pubParasInfo.UserID;
                attachDA.InsertAttach(contractInfo.AttachInfos, this.dataBaseID[0]);
                foreach (AttachDetailInfo info in contractInfo.AttachInfos.AttachDetailInfos)
                {
                    info.AttachID = strContractID + "CRT";
                    info.AttachName = DateTime.Now.ToString("yyyyMMddhhssmm") + "." + info.AttachType;
                    info.CreatedBy = this.pubParasInfo.UserID;
                    attachDetailDA.InsertAttachDetail(info, this.dataBaseID[0]);
                }
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<ContractInfo> ContractInfos = new List<ContractInfo>();
                ContractInfo contractInfo = null;
                string DID = IDS[0];
                DID = DID + "CRT";
                List<string> DIS = new List<string> {
                    DID
                };
                foreach (string id in IDS)
                {
                    contractInfo = contractDA.GetContractByID(dataBase, tran, id, this.dataBaseID[0]);
                    ContractInfos.Add(contractInfo);
                }
                ContractInfos[0].RentWhDetailInfos = rentWhDetailDA.GetRentWhDetailByID(IDS[0], this.dataBaseID[0]);
                List<AttachDetailInfo> attachDetailInfos = attachDetailDA.GetAttachDetailListByID(DID, this.dataBaseID[0]);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if ((ContractInfos[0].RentWhDetailInfos != null) && (ContractInfos[0].RentWhDetailInfos.Count > 0))
                {
                    rentWhDetailDA.DeleteRentWhDetailByID(dataBase, tran, IDS[0], this.dataBaseID[0]);
                }
                if (attachDetailInfos.Count > 0)
                {
                    attachDetailDA.DeleteAttachDetail(DID, this.dataBaseID[0]);
                    attachDA.DeleteAttach(DIS, this.dataBaseID[0]);
                }
                if (contractDA.DeleteContract(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<ContractInfo>(dataBase, tran, ContractInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CONTRACT", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return contractDA.GetAllContract(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            ContractInfo contractInfo = contractDA.GetContractByID(id, this.dataBaseID[0]);
            if (contractInfo != null)
            {
                contractInfo.RentWhDetailInfos = rentWhDetailDA.GetRentWhDetailByID(id, this.dataBaseID[0]);
                contractInfo.AttachInfos = attachDA.GetAttachByID(id + "CRT", this.dataBaseID[0]);
                if (contractInfo.AttachInfos != null)
                {
                    contractInfo.AttachInfos.AttachDetailInfos = attachDetailDA.GetAttachDetailListByID(id + "CRT", this.dataBaseID[0]);
                }
            }
            return contractInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            ContractQueryEntity contractInfo = obj as ContractQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(contractInfo.ContractID))
            {
                sqlWhere.Add(" Contract_ID " + contractInfo.ContractID);
            }
            if (!string.IsNullOrEmpty(contractInfo.OwnerID))
            {
                sqlWhere.Add(" Owner_ID " + contractInfo.OwnerID);
            }
            if (!string.IsNullOrEmpty(contractInfo.ContractType))
            {
                sqlWhere.Add(" Contract_Type " + contractInfo.ContractType);
            }
            if (!string.IsNullOrEmpty(contractInfo.StartTime))
            {
                sqlWhere.Add(" Start_Time " + contractInfo.StartTime);
            }
            if (!string.IsNullOrEmpty(contractInfo.EndTime))
            {
                sqlWhere.Add(" End_Time " + contractInfo.EndTime);
            }
            if (!string.IsNullOrEmpty(contractInfo.IsActive))
            {
                sqlWhere.Add(" Is_Active " + contractInfo.IsActive);
            }
            if (!string.IsNullOrEmpty(contractInfo.IsAudit))
            {
                sqlWhere.Add(" Is_Audit " + contractInfo.IsAudit);
            }
            return contractDA.GetContractByQueryList(sqlWhere, contractInfo, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            ContractInfo contractInfo = obj as ContractInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(contractInfo.ContractID) && contractDA.CheckContractIDUnique(dataBase, tran, contractInfo.ContractID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9110_002", this.pubParasInfo.UserLanguage, new List<string> { contractInfo.ContractID });
                }
                if (!ownerDA.CheckTradePartnerIDUnique(dataBase, tran, contractInfo.OwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9110_001", this.pubParasInfo.UserLanguage, new List<string> { contractInfo.OwnerID });
                }
                if (string.IsNullOrEmpty(contractInfo.ContractID))
                {
                    contractInfo.ContractID = commonMethod.GenNextNumStr(dataBase, tran, "CONTRACT", whLoginID);
                }
                contractInfo.UpdatedBy = contractInfo.CreatedBy;
                contractInfo.CreatedDate = DateTime.Now;
                contractInfo.UpdatedDate = DateTime.Now;
                if (contractDA.InsertContract(dataBase, tran, contractInfo, whLoginID) > 0)
                {
                    strContractID = contractInfo.ContractID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "CONTRACT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            this.Copy(strContractID, contractInfo);
            return strContractID;
        }

        public string Update(object obj)
        {
            ContractInfo contractInfo = obj as ContractInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                ContractInfo oldContractInfo = contractDA.GetContractByID(dataBase, tran, contractInfo.ContractID, this.dataBaseID[0]);
                if (contractDA.UpdateContract(dataBase, tran, contractInfo, whLoginID) > 0)
                {
                    strContractID = contractInfo.ContractID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, oldContractInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "CONTRACT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }
    }
}

